/*
 * ArtifactResolutionService
 * 
 * Version information
 * 
 * Feb 25, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.webapp.struts.action;

import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;

import net.amsoft.iservice.isso.service.ISSOService;
import net.amsoft.iservice.isso.util.SAMLProcessor;

import org.apache.log4j.Logger;
import org.apache.soap.util.xml.XMLParserUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.xmlbeans.XmlObject;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
 * @author rahul
 *  
 */
public class ArtifactResolutionService extends Action
{
	private static Logger logger = Logger.getLogger(ArtifactResolutionService.class);
    public ActionForward execute(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse)
    {
    	logger.info("execute() : entry");
    	try
        {
            if (oRequest.getContentLength() > 0)
            {
            	logger.info("ProcessingContact=true");
            	BufferedReader oReader = oRequest.getReader();
                DocumentBuilder oDocumentBuilder = XMLParserUtils
                        .getXMLDocBuilder();
                Document oDocument = oDocumentBuilder.parse(new InputSource(
                        oReader));
                XmlObject oXmlObj = SAMLProcessor
                        .getArtifactResolveDocument(oDocument);
                String sArtifact = SAMLProcessor
                        .deserializeArtifactResolveDocument(oXmlObj);
                logger.debug("Artifact=" + sArtifact);
                                
                String sResponse = ISSOService.getSAMLResponse(sArtifact);
                String sArtifactResponseXML = SAMLProcessor
                        .serializeArtifactResponseDocument(oXmlObj.xmlText(),
                                oRequest.getServerName(), sResponse);
                logger.info("SerializeResponse=true");
                                
                String sSOAPPrefix = "<?xml version='1.0' encoding='UTF-8'?>"
                        + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
                        + "xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"><SOAP-ENV:Body>";
                String sSOAPSuffix = "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
                String sSOAPMsg = sSOAPPrefix + sArtifactResponseXML
                        + sSOAPSuffix;
                OutputStream oOutStream = oResponse.getOutputStream();
                PrintWriter oPrintWriter = new PrintWriter(oOutStream);
                oPrintWriter.write(sSOAPMsg);
                oPrintWriter.flush();
                oOutStream.flush();
                oPrintWriter.close();
                oOutStream.close();
                logger.info("Stream=closed");                
            }
            logger.info("execute() : exit");
            return null;
        } catch (Exception e)
        {
        	logger.error("execute() : Exception :" + e.getMessage());
        	logger.info("execute() : exit");
            return null;
        }
    }
}
